<?php

namespace app\common\model\market;

use app\common\model\TimeModel;


class SeckillTime extends TimeModel
{
    protected $deleteTime = 'delete_time';

    /**
     * 修改器
     * @param $value
     * @param $data
     */
    public function setDayTimeAttr($value, $data)
    {
        $mons = explode(' - ',$data['mon_time']);
        $this->set('start_time', $value.' '.$mons[0]);
        $this->set('end_time', $value.' '.$mons[1]);
        return $value;
    }

    //获取秒杀商品
    public function SeckillItem()
    {
        return $this->belongsToMany('app\common\model\market\MarketItem','relation_seckill','other_id','time_id');

    }
    /**
     * 新增后置操作
     */
    public static function onAfterInsert($data)
    {
        /*$seckill_times = app('app\common\model\market\SeckillTime');
        $date = explode(' - ',$data['day_time']);
        $times = self::getDateFromRange($date[0],$date[1]);
        $mons = explode(' - ',$data['mon_time']);
        $times_list = [];
        foreach($times as $k=>$v){
          $times_list[$k]['time_id'] = $data['id'];
          $times_list[$k]['name'] = $data['name'];
          $times_list[$k]['start_time'] = $v.' '.$mons[0];
          $times_list[$k]['end_time'] = $v.' '.$mons[1];
        }
        $seckill_times->saveAll($times_list);*/

    }

    /**
     * 查询前置操作
     */
    public static function onBeforeSelect($data)
    {

        $relation_seckill = app('app\common\model\market\RelationSeckill');

    }

    /**
     * 获取指定日期段内每一天的日期
     * @param Date $startdate 开始日期
     * @param Date $enddate  结束日期
     * @return Array
     */
    public static function getDateFromRange($startdate, $enddate){
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400+1;
        // 保存每天日期
        $date = array();
        for($i=0; $i<$days; $i++){
            $date[] = date('Y-m-d', $stimestamp+(86400*$i));
        }
        return $date;
    }
}
?>